package com.woniuxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.domain.Project;
import com.woniuxy.domain.Recordbug;
import com.woniuxy.dto.*;
import com.woniuxy.exception.BugListException;
import com.woniuxy.parameter.DeleteBugParam;
import com.woniuxy.parameter.InsertBugParam;
import com.woniuxy.parameter.PageParam;
import com.woniuxy.parameter.UpdateBugParam;
import com.woniuxy.service.ProjectService;
import com.woniuxy.service.RecordbugService;
import com.woniuxy.shiro.JWTUtils;
import com.woniuxy.util.BeanCopyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName BugLIstController
 * @Description 缺陷列表控制类
 * @Author 王昊波
 * @Version 1.0
 */
@RestController
@RequestMapping("/bugList")
@Api(tags = "缺陷列表接口")
@Slf4j
@CrossOrigin
public class BugListController {
    @Autowired
    private RecordbugService recordbugService;
    @Autowired
    private ProjectService projectService;

    /**
     *@Author 王昊波
     *@Description 查询所有的缺陷，可以模糊查询并分页
     *@Param [pageParam, bis] 分页参数，缺陷模糊查询参数
     *@return com.woniuxy.dto.JSONResult
     */
    @GetMapping("/select")
    @ApiOperation("查询缺陷列表接口")
    @RequiresPermissions({"缺陷列表"})
    public DtoJSONResult selectBugList(PageParam pageParam, DtoBugListSelect bis) throws Exception{
        System.out.println(bis);
        Page<Recordbug> bugPage = new Page<>(pageParam.getPageNum(),pageParam.getPageSize());
        QueryWrapper<Recordbug> wrapper = new QueryWrapper<>();
        if(null != bis.getStartTime()){
            wrapper.ge("pendtime",bis.getStartTime());
        }
        if(null != bis.getEndTime()){
            wrapper.le("pendtime",bis.getEndTime());
        }
        if(null != bis.getProjectName() && !"".equals(bis.getProjectName())){
            wrapper.eq("project_name",bis.getProjectName());
        }
        if(null != bis.getStatus() && !"".equals(bis.getStatus())){
            wrapper.eq("dostate",bis.getStatus());
        }
        recordbugService.page(bugPage, wrapper);
        List<Recordbug> records = bugPage.getRecords();
        List<DtoBugList> bugLists = BeanCopyUtil.copyListProperties(records, DtoBugList::new);
        for (int i = 0; i < bugLists.size(); i++) {
            List list = new ArrayList();
            list.add(bugLists.get(i).getPstartetime());
            list.add(bugLists.get(i).getPendtime());
            bugLists.get(i).setStartAndEndTime(list);
        }
        System.out.println(bugLists);
        DtoPageInfo dtoPageInfo = new DtoPageInfo();
        dtoPageInfo.setTotal(bugPage.getTotal());
        dtoPageInfo.setPages(bugPage.getPages());
        dtoPageInfo.setCurrent(bugPage.getCurrent());
        dtoPageInfo.setSize(bugPage.getSize());
        return new DtoJSONResult(DtoJSONResultCode.SELECTBUGLISTSUCCESS.getCode(), DtoJSONResultCode.SELECTBUGLISTSUCCESS.getMessage(), dtoPageInfo,bugLists);
    }
    /**
     *@Author 王昊波
     *@Description 新增缺陷
     *@Param [insertBugParam] 新增缺陷参数
     *@return com.woniuxy.dto.JSONResult
     */
    @PostMapping("/insert")
    @ApiOperation("新增bug接口")
    @RequiresPermissions({"缺陷列表"})
    public DtoJSONResult insertBug(InsertBugParam insertBugParam, HttpServletRequest request) throws Exception{
        String token = request.getHeader("token");
        int id = Integer.parseInt(JWTUtils.getId(token));
        Recordbug recordbug = new Recordbug();
        BeanUtils.copyProperties(insertBugParam,recordbug);
        recordbug.setUserId(id);
        QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<Project>();
        projectQueryWrapper.eq("project_name",insertBugParam.getProjectName());
        Project project = projectService.getOne(projectQueryWrapper);
        recordbug.setProjectId(project.getProjectId());
        recordbug.setCreateTime(new Date());
        boolean save = recordbugService.save(recordbug);
        if(!save){
            throw new BugListException(DtoBuglistResponse.INSERTBUGERROR.getCode(), DtoBuglistResponse.INSERTBUGERROR.getMessage());
        }
        return new DtoJSONResult(DtoBuglistResponse.INSERTBUGSUCCESS.getCode(), DtoBuglistResponse.INSERTBUGSUCCESS.getMessage(),null,null);
    }
    /**
     *@Author 王昊波
     *@Description 根据id删除bug
     *@Param [deleteBugParam]
     *@return com.woniuxy.dto.JSONResult
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除bug接口")
    @RequiresPermissions({"缺陷列表"})
    public DtoJSONResult deleteBug(DeleteBugParam deleteBugParam) throws Exception{
        boolean b = recordbugService.removeByIds(deleteBugParam.getBugIds());
        if(!b)
            throw new BugListException(DtoBuglistResponse.DELETEBUGERROR.getCode(), DtoBuglistResponse.DELETEBUGERROR.getMessage());
        return new DtoJSONResult(DtoBuglistResponse.DELETEBUGSUCCESS.getCode(), DtoBuglistResponse.DELETEBUGSUCCESS.getMessage(),null,null);
    }
    /**
     *@Author 王昊波
     *@Description 根据id修改bug
     *@Param [updateBugParam] 修改bug参数
     *@return com.woniuxy.dto.JSONResult
     */
    @PostMapping("/update")
    @ApiOperation("修改bug接口")
    @RequiresPermissions({"缺陷列表"})
    public DtoJSONResult updateBug(UpdateBugParam updateBugParam) throws Exception{
        System.out.println(updateBugParam);
        Recordbug recordbug = new Recordbug();
        BeanUtils.copyProperties(updateBugParam,recordbug);
        QueryWrapper<Recordbug> recordbugQueryWrapper = new QueryWrapper<Recordbug>();
        recordbugQueryWrapper.eq("bug_id",updateBugParam.getBugId());
        boolean update = recordbugService.update(recordbug, recordbugQueryWrapper);
        if(!update)
            throw new BugListException(DtoBuglistResponse.UPDATEBUGERROR.getCode(), DtoBuglistResponse.UPDATEBUGERROR.getMessage());
        return new DtoJSONResult(DtoBuglistResponse.UPDATEBUGSUCCESS.getCode(), DtoBuglistResponse.UPDATEBUGSUCCESS.getMessage(), null,null);
    }
}
